<template>
  <div class="page-container">
    <div class="tool-container">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item prop="regionCode">
          <RegionSelect v-model="queryParams.regionCode" placeholder="行政区域" clearable />
        </el-form-item>
        <el-form-item prop="name">
          <el-input v-model="queryParams.name" placeholder="酒店名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['biz:contract:list']">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="tool-handle">
        <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['biz:contract:add']">新增合同</el-button>
      </div>
    </div>
    <div class="tabel-container">
      <vxe-grid ref="tableRef" v-bind="gridOptions">
        <template #status="{ row }">
          <dict-tag :options="contract_status" :value="row.status" />
        </template>

        <template #handle="{ row }">
          <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['biz:contract:query']">详情</el-button>
          <el-button link type="primary" @click="handleEdit(row)" v-hasPermi="['biz:contract:edit']">编辑</el-button>
          <el-button
            link
            type="danger"
            v-show="row.status == 2 || row.status == 1 || row.status == 3"
            @click="handleCancel(row)"
            v-hasPermi="['biz:contract:edit']"
            >作废</el-button
          >
          <el-button link type="primary" v-show="row.status == 1" @click="handleTake(row)" v-hasPermi="['biz:contract:edit']">生效</el-button>
        </template>
        <template #bottom>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </template>
      </vxe-grid>
    </div>
  </div>
</template>

<script setup lang="ts">
import { listContract, invalidateContract, effectiveContract, getVendorContract } from '@/api/operation/contract/index';
import { BizContractVo } from '@/api/operation/contract/types';
import { getTableConfig } from './columns';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { contract_status } = toRefs<any>(proxy?.useDict('contract_status'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  name: '',
  regionCode: ''
});

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listContract(queryParams.value);
  let datas=res.rows;
  datas.forEach(data=>{
data.signTime=data.signTime?.substring(0,10);
data.validFrom=data.validFrom?.substring(0,10);
data.validTo=data.validTo?.substring(0,10);
  });
  data.value = datas;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 新增 */
const handleAdd = () => {
  proxy?.$tab.openPage('/operation/operContract/add', '新增合同');
};

/**
 * 编辑
 */
const handleEdit = (row) => {
  proxy?.$tab.openPage('/operation/operContract/add',"编辑合同", {id:row.contractId});
};


/** 作废 */
const handleCancel = async (row?: BizContractVo) => {
  // let res = await getVendorContract(row?.vendorId || '')
  // console.log(res)
  const _ids = row?.contractId || '';
  await proxy?.$modal.confirm('酒店"' + row?.vendorName + '"当前无新生效合同，是否确定作废？');
  // await proxy?.$modal.confirm('是否确认作废合同"' + row?.contractNo + '"的数据项？')
  loading.value = true;
  await invalidateContract(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('作废成功');
};
/** 生效 */
const handleTake = async (row?: BizContractVo) => {
  const _ids = row?.contractId || '';
  await proxy?.$modal.confirm('是否确认生效合同"' + row?.contractNo + '"的数据项？');
  loading.value = true;
  await effectiveContract(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('生效成功');
};
/** 详情按钮操作 */
const handleDetail = async (row?: BizContractVo) => {
  proxy?.$tab.openPage('/operation/operContract/info', '', { id: row?.contractId });
};

onMounted(() => {
  getList();
});
</script>
